import { bindable, containerless } from 'aurelia-framework';

@containerless
export class EmProjectEditMember {

    @bindable project;
    users = nsCtx.users;
    members = [];

    /**
     * 构造函数
     */
    constructor() {
        this.membersOpts = {
            fullTextSearch: true,
            onAdd: (addedValue, addedText, $addedChoice) => {
                $.post('/admin/task/project/member/add', {
                    id: this.project.id,
                    users: addedValue,
                    // baseUrl: utils.getBaseUrl(),
                    // path: wurl('path'),
                }, (data, textStatus, xhr) => {
                    if (data.success) {
                        toastr.success('添加成员成功!');
                        this.project.members = data.data.members;
                        ea.publish(nsCons.EVENT_TASK_PROJECT_MEMBER_ADD_OR_REMOVE, {
                            type: 'add',
                            members: data.data.members
                        });
                    } else {
                        toastr.error(data.data, '添加成员失败!');
                    }
                }).always(() => {});
            },
            onLabelRemove: (removedValue) => {
                if (this.project.leader.username == removedValue) {
                    return false;
                }

                $.post('/admin/task/project/member/remove', {
                    id: this.project.id,
                    users: removedValue,
                    // baseUrl: utils.getBaseUrl(),
                    // path: wurl('path'),
                }, (data, textStatus, xhr) => {
                    if (data.success) {
                        toastr.success('移除成员成功!');
                        this.project.members = data.data.members;
                        ea.publish(nsCons.EVENT_TASK_PROJECT_MEMBER_ADD_OR_REMOVE, {
                            type: 'remove',
                            members: data.data.members
                        });
                    } else {
                        toastr.error(data.data, '移除成员失败!');
                    }
                }).always(() => {});
            }
        };

        // this.subscribe = ea.subscribe(nsCons.EVENT_TASK_PROJECT_EDIT_TAB_CHANGED, (payload) => {
        // 	toastr.success(payload.tab);
        //     if (payload.tab == 'project-member') {
        //         // $(this.membersRef).dropdown().dropdown('clear');
        //         // console.log(this.project);
        //         // this.members = this.project.members;
        //         // this.projectChanged();
        //     }
        // });
    }

    /**
     * 当数据绑定引擎从视图解除绑定时被调用
     */
    unbind() {
        // this.subscribe.dispose();
    }

    projectChanged() {
        if (this.project) {
            let usernames = _.sortBy(_.map(this.project.members, 'username'));
            _.defer(() => {
                $(this.membersRef).dropdown().dropdown('clear').dropdown('set selected', usernames).dropdown(this.membersOpts);
            });

        }
    }

    initMembersUI(last) {

        if (last) {
            _.defer(() => {
                this.projectChanged();
            });
        }
    }

}
